package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: gmall-parent
 * @author: Mr.Zhuo
 * @create: 2022-04-03 16:23
 **/
@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper,BaseCategoryTrademark> implements BaseCategoryTrademarkService {

    @Autowired
    private BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;

    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;

    //删除分类品牌关联
    @Override
    public void myRemove(Long category3Id, Long trademarkId) {
        //  逻辑删除： 本质更新操作 is_deleted
        //  更新： update base_category_trademark set is_deleted = 1 where category3_id=? and trademark_id=?;
        QueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkQueryWrapper = new QueryWrapper<>();
        baseCategoryTrademarkQueryWrapper.eq("category3_id",category3Id);
        baseCategoryTrademarkQueryWrapper.eq("trademark_id",trademarkId);
        baseCategoryTrademarkMapper.delete(baseCategoryTrademarkQueryWrapper);
    }

    //保存分类品牌关联
    @Override
    public void save(CategoryTrademarkVo categoryTrademarkVo) {
        // 获取所有的品牌id值
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();

        if (!CollectionUtils.isEmpty(trademarkIdList)) {
            // 数据不为空
            // 进行数据转型
            List<BaseCategoryTrademark> categoryTrademarks = trademarkIdList.stream().map(trmId -> {
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
                baseCategoryTrademark.setTrademarkId(trmId);

                return baseCategoryTrademark;
            }).collect(Collectors.toList());
            // 然后进行批量添加
            this.saveBatch(categoryTrademarks);
        }


    }

    //根据category3Id获取可选品牌列表
    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {

        // 首先获取所有的品牌数目
        List<BaseTrademark> baseTrademarks = baseTrademarkMapper.selectList(null);

        // 获取跟category3Id绑定之后的品牌数目
        //List<BaseTrademark> trademarkList = this.findTrademarkList(category3Id);
        QueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkQueryWrapper = new QueryWrapper<>();
        baseCategoryTrademarkQueryWrapper.eq("category3_id", category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(baseCategoryTrademarkQueryWrapper);


        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)) {
            //  需要获取到这个集合中的品牌Id 集合数据
            List<Long> tradeMarkIdList = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> {
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());


            // 循环遍历
            List<BaseTrademark> list = baseTrademarks.stream().filter(baseTrademark -> {
                return !tradeMarkIdList.contains(baseTrademark.getId());
            }).collect(Collectors.toList());
            return list;


        }
        return baseTrademarks;
    }

    //根据category3Id获取品牌列表
    @Override
    public List<BaseTrademark> findTrademarkList(Long category3Id) {

        //  根据分类Id 获取到品牌Id 集合数据
        //  select * from base_category_trademark where category3_id = ?;
        QueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkQueryWrapper = new QueryWrapper<>();
        baseCategoryTrademarkQueryWrapper.eq("category3_id", category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(baseCategoryTrademarkQueryWrapper);

        //  判断baseCategoryTrademarkList 这个集合
        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)) {
            //  需要获取到这个集合中的品牌Id 集合数据
            List<Long> tradeMarkIdList = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> {
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());
            //  正常查询数据的话... 需要根据品牌Id 来获取集合数据！
            return baseTrademarkMapper.selectBatchIds(tradeMarkIdList);
        }
        //  如果集合为空，则默认返回空
        return null;

    }
}
